/*
 * XEditor.java, created on Nov 12, 2005
 * 
 * Copyright (c) 2005 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.editor;

import javax.swing.Icon;
import javax.swing.JComponent;

import org.xfc.util.XListenerManager;

// TODO: have a valid property

// TODO: have an invalid / error message ?

// TODO: use validators instead?

// TODO: have a 'required' property


/**
 *
 *
 * @author Devon Carew
 */
public abstract class XEditor
	extends JComponent
{
	private XListenerManager 	listenerManager = new XListenerManager();
	
	private Object 				oldEditorValue;
	
	protected boolean 			modified;
	protected String			title;
	protected Icon				icon;
	
	/**
	 * 
	 */
	public XEditor()
	{
		this(null, null);
	}
	
	/**
	 * 
	 * 
	 * @param title
	 */
	public XEditor(String title)
	{
		this(title, null);
	}
	
	/**
	 * 
	 * 
	 * @param title
	 * @param icon
	 */
	public XEditor(String title, Icon icon)
	{
		setTitle(title);
		setIcon(icon);
	}
	
	/**
	 * @return the value for this editor
	 */
	public abstract Object getValue();
	
	/**
	 * @param value
	 */
	public abstract void setValue(Object value);
	
	/**
	 * @return whether this editor has been modified
	 */
	public boolean isModified()
	{
		return modified;
	}
	
	/**
	 * @param modified
	 */
	public void setModified(boolean modified)
	{
		boolean oldValue = this.modified;
		this.modified = modified;
		if (!modified)
			oldEditorValue = null;
		firePropertyChange("modified", oldValue, this.modified);
	}
	
	/**
	 * @return Returns the icon.
	 */
	public Icon getIcon()
	{
		return icon;
	}
	
	/**
	 * @param icon The icon to set.
	 */
	public void setIcon(Icon icon)
	{
		Icon oldValue = this.icon;
		this.icon = icon;
		firePropertyChange("icon", oldValue, this.icon);
	}
	
	/**
	 * @return Returns the title.
	 */
	public String getTitle()
	{
		return title;
	}
	
	/**
	 * @param title The title to set.
	 */
	public void setTitle(String title)
	{
		String oldValue = this.title;
		this.title = title;
		firePropertyChange("title", oldValue, this.title);
	}
	
	/**
	 * 
	 */
	public void clear()
	{
		setValue(null);
	}
	
	/**
	 * @param listener
	 */
	public void addEditorListener(XEditorListener listener)
	{
		listenerManager.addListener(listener);
	}
	
	/**
	 * @param listener
	 */
	public void removeEditorListener(XEditorListener listener)
	{
		listenerManager.removeListener(listener);
	}
	
	/**
	 * 
	 */
	protected void fireEditorEvent()
	{
		Object newValue = getValue();
		
		fireEditorEvent(new XEditorEvent(this, oldEditorValue, newValue));
		
		oldEditorValue = newValue;
	}
	
	/**
	 * @param event
	 */
	protected void fireEditorEvent(XEditorEvent event)
	{
		listenerManager.notifyListeners("valueChanged", event);
	}
	
}
